// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_render_window.h"
#include "mk_engine.h"
#include "mk_profiler.h"
#include "mk_render_device.h"
#include "mk_painter.h"

namespace MK3DE
{
	void CRenderWindow::SetWindowHandle(void *handle)
	{
		_handle = handle;
		OnResized();
	}

	void CRenderWindow::OnResized()
	{
#ifdef WIN32
		HWND hWnd = (HWND)_handle;
		RECT rc;
		GetClientRect(hWnd, &rc);

		_width = (DWORD)(rc.right - rc.left);
		_height = (DWORD)(rc.bottom - rc.top);
#endif
	}

	void CRenderWindow::Render()
	{
		CEngine* engine = GetEngine();
		CRenderDevice* renderDevice = engine->GetRenderDevice();
		CPainter* painter = engine->GetPainter();
		renderDevice->SetViewport(0, 0, _width, _height, 0, 1.0f);
		renderDevice->BeginFrame();

#if MK_PROFILER_ENABLE
		CProfiler* profiler = engine->GetProfiler();
		profiler->BeginFrame();
#endif

		CRenderFrame::Render();

#if MK_PROFILER_ENABLE
		profiler->ShowStatistics();
		profiler->EndFrame();
#endif

		painter->Flush();
		renderDevice->EndFrame();
		renderDevice->Present(this);
	}
}
